package testcase.command;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.NoSuchElementException;

import org.junit.Test;

import satir.card.Choice;
import satir.card.Question;
import satir.command.AskQuestionCmd;
import satir.player.Player;

import junit.framework.TestCase;


public class In_AskQuestionCmd_1 extends TestCase{
	/*
	 * Test Case: 1
	 * Objective: Test Integration of AskQuestionCmd, Player, Question and Choice, to print out the question of event Q1 and those choices of event card one
	 */
	@Test 
	public void testQuestion1PrintOut(){

		ArrayList<Choice> choices;
		choices = new ArrayList<Choice>();
		choices.add(new Choice("Blame him/her and prohibit use the phone more than 5 mins.", 1, new Hashtable<String, Integer>()));
		choices.add(new Choice("Talk with son/daughter and find the reason.", 1, new Hashtable<String, Integer>()));
		choices.add(new Choice("Threaten son/daughter. If the situation still not change, you will modify the amount of pocket money.", -1, new Hashtable<String, Integer>()));
		choices.add(new Choice("Ignore him/her.", -1, new Hashtable<String, Integer>()));

		String question1 = "Mother notices her children talk with classmate at night about 30 mins or more. As Father, what is your decision?";
		Player player = new Player();
		Question question = new Question(question1, choices);
		AskQuestionCmd askQuestionCmd = new AskQuestionCmd(question, player);

		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		PrintStream original = System.out;
		System.setOut(new PrintStream(systemOut)); // Start store the output here
		String input = "1";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		try {
			askQuestionCmd.execute();
		} catch (NoSuchElementException e) {
		}
		System.setOut(original); // Store the output unit here
		
		String actualResult = systemOut.toString();
		int startIndex = 0;
		String actualQuestion = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice1 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice2 =actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice3 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice4 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		
		boolean result = actualQuestion.equals("Mother notices her children talk with classmate at night about 30 mins or more. As Father, what is your decision?")
						&& actualChoice1.equals("1. Blame him/her and prohibit use the phone more than 5 mins.")
						&& actualChoice2.equals("2. Talk with son/daughter and find the reason.")
						&& actualChoice3.equals("3. Threaten son/daughter. If the situation still not change, you will modify the amount of pocket money.")
						&& actualChoice4.equals("4. Ignore him/her.");
		assertEquals(true,result);
	}
	
	/*
	 * Test Case 2
	 * Objective: Test Integration of AskQuestionCmd, Player, Question and Choice, to print out the question of event Q1 and those choices of event card two 
	 */
	@Test
	public void testPrintQuestion2(){ 
		ArrayList<Choice> choices;
		choices = new ArrayList<Choice>();
		choices.add(new Choice("You decide to go and lie to parent.", -2, new Hashtable<String, Integer>()));
		choices.add(new Choice("Talk with son/daughter and find the reason.", 1, new Hashtable<String, Integer>()));
		choices.add(new Choice("You will discuss with parent about the revision time.", 5, new Hashtable<String, Integer>()));
		choices.add(new Choice("Ask bother or sister to give advice.", 1, new Hashtable<String, Integer>()));
		
		String question2 = "Your children is facing the examination, Father prohibits his/her go outside since his/her Chinese subject result is only got 60 marks\nBut he/she is invited to attend the brithday party on this Saturday. As childern, What is your decision?";
		Player player = new Player();
		Question question = new Question(question2, choices);
		AskQuestionCmd askQuestionCmd = new AskQuestionCmd(question, player);
				
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		PrintStream original = System.out;
		System.setOut(new PrintStream(systemOut)); // Start store the output here
		String input = "1";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		try {
			askQuestionCmd.execute();
		} catch (NoSuchElementException e) {
		}
		System.setOut(original); // Store the output unit here
		
		String actualResult = systemOut.toString();
		int startIndex = 0;
		int Index = actualResult.indexOf('\n', startIndex);
		String actualQuestion = actualResult.substring(startIndex, actualResult.indexOf('\n', Index+1)-1);
		startIndex = actualResult.indexOf('\n', Index+1)+1;
		String actualChoice1 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice2 =actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice3 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice4 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		
		boolean result = actualQuestion.equals("Your children is facing the examination, Father prohibits his/her go outside since his/her Chinese subject result is only got 60 marks\nBut he/she is invited to attend the brithday party on this Saturday. As childern, What is your decision?")
						&& actualChoice1.equals("1. You decide to go and lie to parent.")
						&& actualChoice2.equals("2. Talk with son/daughter and find the reason.")
						&& actualChoice3.equals("3. You will discuss with parent about the revision time.")
						&& actualChoice4.equals("4. Ask bother or sister to give advice.");
		assertEquals(true,result);
	}
	
}
